package su.order.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import su.base.dao.SuBaseDAO;
import su.order.dao.IOrderProductDAO;
import su.order.entity.OrderProductEntity;


@Repository
public class OrderProductDAO extends SuBaseDAO implements IOrderProductDAO {
	
	
	public List<OrderProductEntity> getAllByOrderId(int orderId) throws SQLException {
		String sql = "select * from su_order_product where order_id=? ORDER BY id desc";
		
		return super.query(sql,new Object[]{orderId}, new RowMapper<OrderProductEntity>() {

			public OrderProductEntity mapRow(ResultSet rs, int arg1) throws SQLException {
				OrderProductEntity entity = new OrderProductEntity();
				entity.setId(rs.getInt("id"));
				entity.setOrderId(rs.getInt("order_id"));
				entity.setProductId(rs.getInt("product_id"));
				entity.setCustname(rs.getString("custname"));
				entity.setPackname(rs.getString("packname"));
				entity.setNum(rs.getDouble("num"));
				entity.setOpprice(rs.getDouble("opprice"));
				entity.setStorageStatus(rs.getInt("storage_status"));
				entity.setStorageTime(rs.getDate("storage_time"));
				entity.setInvoiceStatus(rs.getInt("invoice_status"));
				entity.setPayment(rs.getDouble("payment"));
				entity.setRealPayment(rs.getDouble("real_payment"));
				entity.setInvoiceTime(rs.getDate("invoice_time"));
				entity.setPackageNum(rs.getInt("package_num"));
				entity.setFreight(rs.getDouble("freight"));
				entity.setTax(rs.getDouble("tax"));
				entity.setMargin(rs.getDouble("margin"));
				entity.setCcprice(rs.getDouble("ccprice"));
				return entity;
			}
			
		});
	}
	
	
	public OrderProductEntity getById(int id) throws SQLException {
		String sql = "select * from su_order_product where id=? limit 1";
		return super.queryForObject(sql, new Object[]{id}, new RowMapper<OrderProductEntity>() {
			public OrderProductEntity mapRow(ResultSet rs, int value) throws SQLException {
				OrderProductEntity entity = new OrderProductEntity();
				entity.setId(rs.getInt("id"));
				entity.setOrderId(rs.getInt("order_id"));
				entity.setProductId(rs.getInt("product_id"));
				entity.setCustname(rs.getString("custname"));
				entity.setPackname(rs.getString("packname"));
				entity.setNum(rs.getDouble("num"));
				entity.setOpprice(rs.getDouble("opprice"));
				entity.setStorageStatus(rs.getInt("storage_status"));
				entity.setStorageTime(rs.getDate("storage_time"));
				entity.setInvoiceStatus(rs.getInt("invoice_status"));
				entity.setPayment(rs.getDouble("payment"));
				entity.setRealPayment(rs.getDouble("real_payment"));
				entity.setInvoiceTime(rs.getDate("invoice_time"));
				entity.setPackageNum(rs.getInt("package_num"));
				entity.setFreight(rs.getDouble("freight"));
				entity.setTax(rs.getDouble("tax"));
				entity.setMargin(rs.getDouble("margin"));
				entity.setCcprice(rs.getDouble("ccprice"));
				return entity;
			}
		});
	}

	
	public void remove(int id) throws SQLException {
		final String sql = "delete from su_order_product where id=?";
		super.update(sql, new Object[]{id});
	}
	
	
	
	public void insert(OrderProductEntity o) throws SQLException {
		String sql = "insert into su_order_product(order_id ,product_id ,custname ,packname ,num ,opprice ,storage_status ,storage_time ,invoice_status ,payment ,real_payment ,invoice_time ,package_num ,freight ,tax ,margin ,ccprice ) values(? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? )";
		super.update(sql, new Object[]{o.getOrderId() ,o.getProductId() ,o.getCustname() ,o.getPackname() ,o.getNum() ,o.getOpprice() ,o.getStorageStatus() ,o.getStorageTime() ,o.getInvoiceStatus() ,o.getPayment() ,o.getRealPayment() ,o.getInvoiceTime() ,o.getPackageNum() ,o.getFreight() ,o.getTax() ,o.getMargin() ,o.getCcprice() });
	}
	
	
	
	public void update(OrderProductEntity o) throws SQLException {
		StringBuffer updateSql = new StringBuffer("update su_order_product set ");
		List<Object> args = new ArrayList<Object>();
		args.add(o.getProductId());
		updateSql.append("product_id=?").append(",");
		args.add(o.getCustname());
		updateSql.append("custname=?").append(",");
		args.add(o.getPackname());
		updateSql.append("packname=?").append(",");
		args.add(o.getNum());
		updateSql.append("num=?").append(",");
		args.add(o.getOpprice());
		updateSql.append("opprice=?").append(",");
		args.add(o.getPayment());
		updateSql.append("package_num=?").append(",");
		args.add(o.getCcprice());
		updateSql.append("ccprice=?").append(",");
			
		
		args.add(o.getId());
			
		
		String sql = updateSql.substring(0, updateSql.length()-1) + " where id = ? "; 
		super.update(sql, args.toArray());
	}
	
	public void updateOrderProductFunctionInfo(OrderProductEntity o) throws SQLException {
		String sql = "update su_order_product set payment=?,freight=?,tax=?,margin=?,gsPayment=? where id=?";
		super.update(sql, new Object[]{o.getPayment(),o.getFreight(),o.getTax(),o.getMargin(),o.getGsPayment(),o.getId()});
	}

}

